package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type Dog struct {
	gorm.Model
	Name  string
	PerID uint
	Per   Per
}
type Per struct {
	gorm.Model
	Name string
}

func main() {
	db, err := gorm.Open("mysql", "root:123456@(127.0.0.1:3306)/gin?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		fmt.Println(err)
	}
	defer db.Close()

	//自动迁移
	//db.AutoMigrate(&Dog{}, &Per{})

	//添加关联关系
	//方式1
	//p := Per{
	//	Model: gorm.Model{
	//		ID: 1,
	//	},
	//	Name: "小明",
	//}
	//d := Dog{
	//	Model: gorm.Model{
	//		ID: 1,
	//	},
	//	Name: "旺财",
	//	Per:  p,
	//}
	//db.Create(&d)

	//方式2
	//db.Create(&Dog{Name: "小黑", Per: Per{
	//	Name: "小张",
	//}})

	//方式3
	//p := Per{
	//	Model: gorm.Model{
	//		ID: 3,
	//	},
	//	Name: "小苗",
	//}
	//d := Dog{
	//	Model: gorm.Model{
	//		ID: 3,
	//	},
	//	Name: "哒哒",
	//}
	//db.Model(&d).Association("Per").Append(&p)

}
